//package com.robot.member.controller;
//
//import java.math.BigDecimal;
//import java.time.LocalDateTime;
//import java.util.HashMap;
//import java.util.Map;
//
//import com.robot.member.pojo.Member;
//import com.robot.member.service.MemberService;
//import io.jsonwebtoken.Claims;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.domain.Page;
//import org.springframework.web.bind.annotation.*;
//
//import com.robot.member.pojo.CashWithdrawal;
//import com.robot.member.service.CashWithdrawalService;
//
//import entity.PageResult;
//import entity.Result;
//import entity.StatusCode;
//
//import javax.servlet.http.HttpServletRequest;
//
///**
// * withdrawal控制器层
// *
// * @author
// */
//@RestController
//@CrossOrigin
//@RequestMapping("/cashWithdrawal")
//public class CashWithdrawalController {
//    @Autowired
//    private MemberService memberService;
//    @Autowired
//    private CashWithdrawalService cashWithdrawalService;
//
//    @Autowired
//    private HttpServletRequest request;
//    /**
//     * member表status状态 0-禁用 1-启用
//     */
//    private String memberStatusEnable = "1";
//
//    /**
//     * 查询全部数据
//     *
//     * @return
//     */
//    @RequestMapping(method = RequestMethod.GET)
//    public Result findAll() {
//        return new Result(StatusCode.SUCCESS_QUERY, cashWithdrawalService.findAll());
//    }
//
//    /**
//     * 根据ID查询
//     *
//     * @param id ID
//     * @return
//     */
//    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
//    public Result findById(@PathVariable String id) {
//        return new Result(StatusCode.SUCCESS_QUERY, cashWithdrawalService.findById(id));
//    }
//
//
//    /**
//     * 分页+多条件查询
//     *
//     * @param searchMap 查询条件封装
//     * @param page      页码
//     * @param size      页大小
//     * @return 分页结果
//     */
//    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
//    public Result findSearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
//        Page<CashWithdrawal> pageList = cashWithdrawalService.findSearch(searchMap, page, size);
//        return new Result(StatusCode.SUCCESS_QUERY, new PageResult<CashWithdrawal>(pageList.getTotalElements(), pageList.getContent()));
//    }
//
//    /**
//     * 根据条件查询
//     *
//     * @param searchMap
//     * @return
//     */
//    @RequestMapping(value = "/search", method = RequestMethod.POST)
//    public Result findSearch(@RequestBody Map searchMap) {
//        return new Result(StatusCode.SUCCESS_QUERY, cashWithdrawalService.findSearch(searchMap));
//    }
//
//
//    /**
//     * 修改
//     *
//     * @param withdrawal
//     */
//    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
//    public Result update(@RequestBody CashWithdrawal withdrawal, @PathVariable String id) {
//        withdrawal.setId(id);
//        cashWithdrawalService.update(withdrawal);
//        return new Result(StatusCode.SUCCESS_UPDATED);
//    }
//
//    /**
//     * 删除
//     *
//     * @param id
//     */
//    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
//    public Result delete(@PathVariable String id) {
//        cashWithdrawalService.deleteById(id);
//        return new Result(StatusCode.SUCCESS_DELETED);
//    }
//
//    /**
//     * 提现
//     *
//     * @param map
//     * @return
//     */
//    @PostMapping
//    public Result addCashWithdrawal(@RequestBody HashMap<String, String> map) {
//        Claims claims = (Claims) request.getAttribute("robot_member");
//        if (claims == null) {
//            return new Result( StatusCode.ERROR_TOKEN_EXPIRED);
//        }
//        Member memberInDb = memberService.findById(claims.getId());
//        if (memberInDb == null) {
//            return new Result(StatusCode.ERROR_USER_DOES_NOTE_EXIST);
//        }
//        if (!memberStatusEnable.equals(memberInDb.getStatus())) {
//            return new Result(StatusCode.ERROR_USER_INVALID);
//        }
//
//        String weChat = "10";
//        String aliPay = "20";
//        String unionPay = "30";
//        String otherPay = "40";
//        String payment = "payment";
//        String beanSpread = "beanSpread";
//        String beanRoyalty = "beanRoyalty";
//        String memberPoints = "memberPoints";
//        if (map.get(payment) != null && !"".equals(map.get(payment))) {
//            if (!weChat.equals(map.get(payment)) && !aliPay.equals(map.get(payment)) && !unionPay.equals(map.get(payment)) && !otherPay.equals(map.get(payment))) {
//                return new Result(false, StatusCode.ERROR, "请选择提现到哪里");
//            }
//        } else {
//            return new Result(false, StatusCode.ERROR, "请选择提现到哪里");
//        }
//
//        CashWithdrawal cashWithdrawal = new CashWithdrawal();
//        LocalDateTime now = LocalDateTime.now();
//        if (map.get(beanSpread) != null && !"".equals(map.get(beanSpread))) {
//            return new Result(false, StatusCode.ERROR, "赠送的码豆不可提现");
//        } else if (map.get(beanRoyalty) != null && !"".equals(map.get(beanRoyalty))) {
//            if (memberInDb.getBeanRoyalty().compareTo(new BigDecimal(0)) == -1) {
//                return new Result(false, StatusCode.ERROR, "码豆数量必须大于0才可以提现");
//            }
//            BigDecimal beanRoyaltyResult = memberInDb.getBeanRoyalty().subtract(new BigDecimal(map.get(beanRoyalty)));
//            if (beanRoyaltyResult.compareTo(new BigDecimal(0)) == -1) {
//                return new Result(false, StatusCode.ERROR, "码豆不足");
//            }
//            memberInDb.setBeanRoyalty(beanRoyaltyResult);
//            cashWithdrawal.setBeanRoyalty(new BigDecimal(map.get(beanRoyalty)));
//        } else if (map.get(memberPoints) != null && !"".equals(map.get(memberPoints))) {
//            //积分
//        } else {
//            return new Result(false, StatusCode.ERROR, "提现失败");
//        }
//
//        cashWithdrawal.setMemberId(memberInDb.getId());
//        cashWithdrawal.setPayment(map.get(payment));
//        cashWithdrawal.setStatus("0");
//        cashWithdrawal.setCreatedAt(now);
//        cashWithdrawal.setUpdatedAt(now);
//        cashWithdrawalService.add(cashWithdrawal);
//        memberInDb.setUpdatedAt(now);
//        memberService.update(memberInDb);
//        return new Result(true,StatusCode.SUCCESS, "提现成功");
//    }
//
//    /**
//     * 查询会员名下的所有提现记录
//     *
//     * @param page
//     * @param size
//     * @return
//     */
//    @GetMapping("{page}/{size}")
//    public Result findCashWithdrawalByMemberId(@PathVariable int page, @PathVariable int size) {
//        Claims claims = (Claims) request.getAttribute("robot_member");
//        if (claims == null) {
//            return new Result( StatusCode.ERROR_TOKEN_EXPIRED);
//        }
//        Member memberInDb = memberService.findById(claims.getId());
//        if (memberInDb == null) {
//            return new Result(StatusCode.ERROR_USER_DOES_NOTE_EXIST);
//        }
//        if (!memberStatusEnable.equals(memberInDb.getStatus())) {
//            return new Result(StatusCode.ERROR_USER_INVALID);
//        }
//
//        Page<CashWithdrawal> pageData = cashWithdrawalService.findAllByMemberIdOrderByCreatedAtDesc(memberInDb.getId(), page, size);
//        return new Result(StatusCode.SUCCESS_QUERY, new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
//    }
//
//}
